package com.hcvsa;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;


import com.until.BusProvider;

import fun.flyee.android.pengshan.services.TickListener;
import fun.flyee.android.pengshan.utils.Logger;
import fun.flyee.android.pengshan.utils.Systems;
import hcvs.audio.AudioCapture;
import hcvs.hcvca.bean.Depart;
import hcvs.hcvca.bean.MeetingRoom;
import hcvs.hcvca.bean.User;
import hcvs.hcvsa.MeetingEvent;
import hcvs.hcvsa.SystemEvent;
import hcvs.hcvsa.TPEvent;

/*
 * 视频基础服务
 * */
public class HCVSAService extends Service implements MeetingEvent, SystemEvent, TPEvent {

    private AudioCapture audioCapture;

    private ServiceBinder serviceBinder = new ServiceBinder();

    public class ServiceBinder extends Binder {

        //音频初始化
        public void audioInit(int channel) {
            if (audioCapture == null) {
                audioCapture = new AudioCapture(data -> {
//                    Logger.i("audio test: current audio capture channel: "+audioCapture.getChannel() + "");
                });
                audioCapture.setChannel(channel);
                MyHCVSA.getInstance().SetAudioAEC(false);
                audioCapture.start();
                Logger.e("initial audio in service");
            }
        }

        //音频反初始化
        public void audioUnit() {
            if (audioCapture != null) {
                Logger.e("release audio in service");
                audioCapture.stopRecord();
                audioCapture = null;
            }
        }

        public void checkTrace() {
            checkTrack();
        }

    }

    public void checkTrack(){

    }

    @Override
    public void onCreate() {
        if (!Systems.getCPUABI().contains("arm")) return;
        MyHCVSA.getInstance().setCMSSystemEventListener(this);
        MyHCVSA.getInstance().setMeetingEventListener(this);
        MyHCVSA.getInstance().setTPEvent(this);
        BusProvider.getInstance().register(this);
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        if (audioCapture != null) {
            audioCapture.stopRecord();
            audioCapture = null;
        }
        try {
            MyHCVSA.getInstance().Logout();
        } catch (Exception e) {
            Logger.e("HC video logout error", e);
        }
        BusProvider.getInstance().unregister(this);
        super.onDestroy();
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return serviceBinder;
    }

    //返回服务器登录的状态
    @Override
    public void OnCMSLoginStatus(int nLoginStatus) {
        MyHCVSA.setLoginStatus(nLoginStatus);
        BusProvider.getInstance().post(new OnCMSLoginStatus(nLoginStatus));
    }

    //返回服务器登录的结果
    @Override
    public void OnCMSLoginResult(int nLoginResult) {
        BusProvider.getInstance().post(new OnCMSLoginResult(nLoginResult));
    }

    //登录服务器完成事件
    @Override
    public void OnCMSLoginEnd() {

    }

    @Override
    public void OnCMSNewVersion(String strNewVersion) {
        // TODO Auto-generated method stub

    }

    //服务器强制你下线了。
    @Override
    public void OnCMSForceUserOffline() {
        // TODO Auto-generated method stub

    }

    //服务器返回流媒体信息
    @Override
    public void OnVTDUConnectInfo(int type, int channelid, String logString) {
        // TODO Auto-generated method stub

    }

    @Override
    public void OnDownloadFileEvent(int sub_command, int data) {
        // TODO Auto-generated method stub

    }

    //服务器返回组织机构
    @Override
    public void OnCMSReturnDepart(Depart depart) {
        // TODO Auto-generated method stub

    }

    //服务器返回用户
    @Override
    public void OnCMSReturnUser(User user) {
        // TODO Auto-generated method stub

    }

    //服务器通知用户上线
    @Override
    public void UserOnline(User user, String ip) {
        Logger.i("VideoUser online: " + user.getName());
        BusProvider.getInstance().post(new UserOnline(user, ip));
    }

    //服务器通知用户下线
    @Override
    public void UserOffline(User user) {
        Logger.i("VideoUser offline: " + user.getName());
        BusProvider.getInstance().post(new UserOffline(user));
    }

    //收到用户发送的消息
    @Override
    public void UserSendMessage(int user_sessionid, int userid, String name,
                                int type, String messageString, int format, int format_size) {
        // TODO Auto-generated method stub
        BusProvider.getInstance().post(messageString);
    }

    //收到用户的视频呼叫请求
    @Override
    public void UserCallInvite(int call_type, int sessionid, int userid,
                               String name) {
        try {
            if (Systems.isSystemPhoneBusy(getApplicationContext())) {
                MyHCVSA.getInstance().getUserSessionMoudle().SendCallBusyToUser(sessionid, call_type);
            } else {
                BusProvider.getInstance().post(new UserCallInvite(call_type, sessionid, userid, name));
            }
        } catch (Exception e) {
            Logger.e("CallInvite", e);
        }
    }

    //用户接受了呼叫
    @Override
    public void UserAcceptCall(int invite_type, int sessionid, int userid,
                               String name) {
        BusProvider.getInstance().post(new UserAcceptCall(invite_type, sessionid, userid, name));
    }

    //用户拒绝了呼叫
    @Override
    public void UserRejectCall(int invite_type, int sessionid, int userid,
                               String name) {
        Logger.i("UserRejectCall");
        BusProvider.getInstance().post(new UserRejectCall(invite_type, sessionid, userid, name));
    }

    //用户正忙
    @Override
    public void UserBusyCall(int invite_type, int sessionid, int userid,
                             String name) {
        Logger.i("UserBusyCall");
        BusProvider.getInstance().post(new UserBusyCall(invite_type, sessionid, userid, name));
    }

    @Override
    public void UserACKCall(int invite_type, int sessionid, int userid,
                            String name) {
        Logger.i("UserACKCall");
    }

    //用户挂断了通话
    @Override
    public void UserHandUpCall(int invite_type, int sessionid, int userid,
                               String name) {
        Logger.i("UserHangupCall");
        BusProvider.getInstance().post(new UserHandUpCall(invite_type, sessionid, userid, name));
    }

    //我接受了呼叫
    @Override
    public void MyAcceptCall(int invite_type, int sessionid, int userid,
                             String name) {
        BusProvider.getInstance().post(new MyAcceptCall(invite_type, sessionid, userid, name));
    }

    /**
     * 返回会议室信息
     *
     * @param meetingRoom
     */
    @Override
    public void ReturnMeetingRoomInfo(MeetingRoom meetingRoom) {
        BusProvider.getInstance().post(new ReturnMeetingRoomInfo(meetingRoom));
    }


    /**
     * 返回登录会议室结果
     *
     * @param nLoginResult {@link SystemEvent#HCVS_LOGIN_RESULT_OK}
     */
    @Override
    public void MeetingLoginResult(int nLoginResult) {
        if (!Systems.getCPUABI().contains("arm")) return;
        MyHCVSA.setMeetingLoginResult(nLoginResult);
        BusProvider.getInstance().post(new MeetingLoginResult(nLoginResult));
    }

    /**
     * 返回登录会议室状态
     *
     * @param nLoginStatus {@link SystemEvent#HCVS_LOGIN_STATUS_NO_LOGIN}
     */
    @Override
    public void MeetingLoginStatus(int nLoginStatus) {
        if (!Systems.getCPUABI().contains("arm")) return;
        MyHCVSA.setMeetingLoginStatus(nLoginStatus);
        BusProvider.getInstance().post(new MeetingLoginStatus(nLoginStatus));
    }


    /**
     * 被强制请出会议室
     */
    @Override
    public void MeetingForceQuit() {
        BusProvider.getInstance().post(new MeetingForceQuit());
    }

    /**
     * 用户邀请我参加会议室
     *
     * @param roomid    会议室ID
     * @param room_name 会议室名称
     * @param user_name 邀请人
     */
    @Override
    public void UserInviteMyJoinMeeting(int roomid, String room_name, String user_name, int i) {
        try {
            if (Systems.isSystemPhoneBusy(getApplicationContext())) {
                MyHCVSA.getInstance().getMeetingMoudle().RejectToJoinMeeting(roomid);
            } else {
                BusProvider.getInstance().post(new UserInviteMyJoinMeeting(roomid, room_name, user_name));
            }
        } catch (Exception e) {
            Logger.e("reject call", e);
        }
    }

    @Override
    public void ReturnDepartEnd() {
        // TODO Auto-generated method stub

    }

    @Override
    public void ReturnUserEnd() {
        // TODO Auto-generated method stub

    }

    @Override
    public void StartRegister(int i, int i1, int i2) {

    }

    @Override
    public void StopRegister(int i, int i1) {

    }

    @Override
    public void UserRegister(int i, int i1, String s) {

    }

    @Override
    public void StartVote(int i, int i1, int i2, String s, int i3, int i4, int i5) {

    }

    @Override
    public void StopVote(int i, int i1) {

    }

    @Override
    public void UserVote(int i, int i1, char c) {

    }

    /*打开了一路通道回调事件*/
    @Override
    public void OpenAVChannel(int type, int id, int channel, int type2, int pVideoData) {
        BusProvider.getInstance().post(new OpenAVChannel(type, id, channel, type2, pVideoData));
    }

    /*关闭了一路通道回调事件*/
    @Override
    public void CloseAVChannel(int type, int id, int channel, int type2) {
        BusProvider.getInstance().post(new CloseAVChannel(type, id, channel, type2));
    }


}
